using Senparc.Weixin.MP.CommonAPIs;
using System;
using System.Collections;
using System.Text;
namespace Senparc.Weixin.MP.Helpers
{
	public class JSSDKHelper
	{
		protected Hashtable Parameters;
		public JSSDKHelper()
		{
			this.Parameters = new Hashtable();
		}
		private void SetParameter(string parameter, string parameterValue)
		{
			if (parameter != null && parameter != "")
			{
				if (this.Parameters.Contains(parameter))
				{
					this.Parameters.Remove(parameter);
				}
				this.Parameters.Add(parameter, parameterValue);
			}
		}
		private void ClearParameter()
		{
			this.Parameters.Clear();
		}
		public static string GetNoncestr()
		{
			Random random = new Random();
			return MD5UtilHelper.GetMD5(random.Next(1000).ToString(), "GBK");
		}
		public static string GetTimestamp()
		{
			return Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds).ToString();
		}
		private string CreateSha1()
		{
			StringBuilder stringBuilder = new StringBuilder();
			ArrayList arrayList = new ArrayList(this.Parameters.Keys);
			arrayList.Sort();
			foreach (string text in arrayList)
			{
				string text2 = (string)this.Parameters[text];
				if (stringBuilder.Length == 0)
				{
					stringBuilder.Append(text + "=" + text2);
				}
				else
				{
					stringBuilder.Append("&" + text + "=" + text2);
				}
			}
			return SHA1UtilHelper.GetSha1(stringBuilder.ToString()).ToString().ToLower();
		}
		private string CreateCardSha1()
		{
			StringBuilder stringBuilder = new StringBuilder();
			ArrayList arrayList = new ArrayList(this.Parameters.Keys);
			arrayList.Sort();
			foreach (string key in arrayList)
			{
				string value = (string)this.Parameters[key];
				stringBuilder.Append(value);
			}
			return SHA1UtilHelper.GetSha1(stringBuilder.ToString()).ToString().ToLower();
		}
		public string GetSignature(string ticket, string noncestr, string timestamp, string url)
		{
			this.ClearParameter();
			this.SetParameter("jsapi_ticket", ticket);
			this.SetParameter("noncestr", noncestr);
			this.SetParameter("timestamp", timestamp);
			this.SetParameter("url", url);
			return this.CreateSha1();
		}
		public string GetAddrSign(string appId, string appSecret, string noncestr, string timestamp, string url)
		{
			this.ClearParameter();
			string parameterValue = AccessTokenContainer.TryGetToken(appId, appSecret, false);
			this.SetParameter("appId", appId);
			this.SetParameter("noncestr", noncestr);
			this.SetParameter("timestamp", timestamp);
			this.SetParameter("url", url);
			this.SetParameter("accesstoken", parameterValue);
			return this.CreateSha1();
		}
		public string GetCardSign(string appId, string appSecret, string locationId, string noncestr, string timestamp, string cardId, string cardType)
		{
			this.ClearParameter();
			this.SetParameter("appId", appId);
			this.SetParameter("appsecret", appSecret);
			this.SetParameter("location_id", locationId);
			this.SetParameter("nonce_str", noncestr);
			this.SetParameter("times_tamp", timestamp);
			this.SetParameter("card_id", cardId);
			this.SetParameter("card_type", cardType);
			return this.CreateCardSha1();
		}
	}
}
